<?php
require_once 'phpmoko/lib/common.php';

require_once 'phpmoko/ui/Widget.php';

class Image extends Widget
{
	private $imageSmall = null;
	private $imageBig = null;
	private $groupId = null;
	private $title = null;
	private static $groups = array();

	function __construct($imageSmall)
	{
		parent::__construct();

		$this->imageSmall = $imageSmall;

		$this->setStyleName('pmk-Image');
	}

	function setUrlBigImage($imageBig)
	{
		require_once 'phpmoko/externals/JQueryFancyBox.php';
		JQueryFancyBox::activate();

		$this->imageBig = $imageBig;
	}

	function setGroupId($groupId)
	{
		$this->groupId = $groupId;
		$this->id = $groupId;
	}

	function setTitle($title)
	{
		$this->title = $title;
	}

	function toString()
	{
		$str = '';

		if ($this->imageBig != null) {
			$str .= '<a';

			if ($this->groupId != null)
				$str .= ' class="' . $this->groupId . '" rel="' . $this->groupId . '"';
			else
				$str .= ' id="' . $this->id . '"';

			$str .= ' href="' . $this->imageBig . '">';
		}

		$str .= '<img src="' . $this->imageSmall . '"';

		if ($this->title != null)
			$str .= ' title="' . htmlEntities($this->title, ENT_QUOTES) . '"';

		if ($this->groupId != null)
			$str .= ' alt="' . htmlEntities($this->groupId, ENT_QUOTES) . '"';

		if ($this->getWidth() != null)
			$str .= ' width="' . $this->getWidth() . '"';

		if ($this->getHeight() != null)
			$str .= ' height="' . $this->getHeight() . '"';

		if ($this->style != null)
			$str .= ' class="' . $this->style . '"';

		$str .= '>';

		if ($this->imageBig != null)
			$str .= '</a>';

		if ($this->imageBig != null) {
			require_once 'phpmoko/ui/RootPanel.php';
			$root = RootPanel::get();

			if ($this->groupId != null) {
				if (!in_array($this->groupId, self::$groups)) {
					$root->addJSOnReadyDocument('$(".' . $this->groupId . '").fancybox({openEffect: \'elastic\', closeEffect: \'elastic\', prevEffect: \'fade\', nextEffect: \'fade\', helpers: { title: { type: \'float\' }, buttons: {}, thumbs: { width: 50, height: 50}}});');

					self::$groups[] = $this->groupId;
				}
			} else {
				$root->addJSOnReadyDocument('$("a#' . $this->id . '").fancybox({\'transitionIn\': \'none\', \'transitionOut\': \'none\', \'titlePosition\': \'over\'});');
			}
		}

		return $str;
	}
}
?>